// 02local/07tableOrder/tableOrder.js
var co = require('../../resource/js/common.js')
var app = getApp()
var _this

Page({
    /*页面的初始数据 */
    data: {
        roundCount: '',
        //菜品显示
        product:'',
        tableOrder: '',
        // 餐位费
        tableCost: '',
        // 菜品价格
        products: '',
        // 订单是否支付
        OrderStatus: 1
    },

    /*生命周期函数--监听页面加载 */
    onLoad: function (options) {
        _this = this
        var obj = JSON.parse(options.obj)
        // console.log("【获取桌订单信息】===", obj)
        var width = wx.getSystemInfoSync().windowWidth
        var cartList = obj.Order_CartList
        var c = []
        for (var key in cartList){
            var s = cartList[key].ProductsPriceX * cartList[key].Qty
            c.push(s)
        }
        this.setData({
            roundCount: (width - 30) / 10,
            product: obj.Order_CartList,
            tableOrder: obj,
            products: c,
            tableCost: obj.Seat_Fee * obj.Guest_Num
        })
        app.Data.Order_ID = obj.Order_ID            //将Order_ID存入全局
    },
    // 加菜
    addProduct: function(){
        var tableOrder = this.data.tableOrder
        wx.navigateTo({
            url: '/02local/03foodInfo/foodInfo?Biz_ID=' + tableOrder.Biz_ID + '&Guest_Num=' + tableOrder.Guest_Num + '&Table_ID=' + tableOrder.Table_ID
        })
    },
    //返回刷新
    onShow: function(){
        if (!co.empty(app.Data.tableOrder)){
            var o = JSON.stringify(app.Data.tableOrder)
            var options = {
                obj: o
            }
            this.onLoad(options)
        }
    },
    onUnload: function(){
        wx.switchTab({
            url: '/01home/01home/home'
        })
    },
    // 支付
    pay: function(){
        //购买分销商金额
        var param = {
            openid: app.Data.userInfo.openid,
            appid: app.Data.app.appid,
            key: 'sUiYuAnTeChNoLoGyHtTpSwWwLs293cC',
            notify_url: 'http://api.igoyp.com/public/weixinpay/notify.php',
            mch_id: '1480163852',
            money: 0.01,
            abstract: '买单'
        }
        co.getShareApi('api/Wxpay/createParam', param, function (res) {
            res.data.success = function (result) {
                _this.upDataOrderStatus()
                console.log('【调起支付成功res】', result)
            }
            res.data.fail = function (result) {
                console.log('【调起支付失败res】', result)
            }
            //调起微信支付
            wx.requestPayment(res.data)
        })
    },
    // 更改订单状态
    upDataOrderStatus: function(){
        var param = {
            Order_ID: app.Data.Order_ID,
            Order_Status: 4
        }
        co.getShareApi('order/Omg/updateMealOrder',param,function(res){
            // console.log("【获取返回信息】===",res)
            if(res.code == 0){
                _this.setData({
                    OrderStatus: 0
                })
            }
        })
    }
})
